package com.barber.controller;

import com.barber.entity.Barber;
import com.barber.service.BarberService;
import com.barber.util.XtUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.Map;


@RestController
@RequestMapping("/barber")
public class BarberController {

    Logger logger = Logger.getLogger(BarberController.class);
    @Autowired
    BarberService barberService;
    @GetMapping("/login")
    String login(HttpServletRequest request, @RequestParam Map map) throws IOException{

        Map m=barberService.login(map);

        logger.debug(m); //{password=lll, phone=111111, usertype=2, userid=3, username=哒玲玲}
        if(m!=null){
            request.getSession().setAttribute("islogin",m);
            m.put("usertype",3);
            return "success";
        }else {
            request.getSession().setAttribute("islogin", null);
            return ("用户名或密码错误");
        }
    }
    @GetMapping("/logout")//登录hash表返回两个数据
    protected void  logout(HttpServletRequest request,HttpServletResponse response, @RequestParam Map map) throws IOException {
        request.getSession().setAttribute("islogin",null);
        response.sendRedirect("bye");

    }
    @PostMapping()
    Map select(@RequestParam Map map, HttpServletRequest req) throws IOException {
        Map m  = (Map) req.getSession().getAttribute("islogin");
        logger.debug(m);//{password=22, adminid=1, usertype=0, fpemail=2966123846@qq.com, username=222}
            logger.debug(map);//{pageIndex=0, sortField=createtime, sortOrder=desc, pageSize=10}
//        map.
        Map b=barberService.getBarbers(map);
        logger.debug(b);
        logger.debug("select success");
        return b;
    }
    @PostMapping("/insert")
   String insert(HttpServletRequest request, HttpServletResponse response,@RequestBody Barber barber )throws IOException
    {


        logger.debug(barber);

        if (barberService.insertBaber(barber))
//           return("OK");
           return("OK");
        else
           return("FAIL");
    }
    @GetMapping("/delete")//spring风格 前端传的是	url : "menu2/delete/"+menuid,//Spring风格
    String delete(HttpServletResponse response, HttpServletRequest request,@RequestParam Map map ) throws IOException {
        logger.debug(map);
        if (barberService.deleteBaber(map)) {
            return ("success!");
        } else {
            return("delete failure!");

        }
    }
    @PostMapping("/update")
   String update(HttpServletRequest request, HttpServletResponse resp,@RequestBody Barber barber) throws IOException {
//
        logger.debug(barber);

        barber.setHireTime(XtUtil.TimeConversion(barber.getHireTime()));
        logger.debug(barber);
        if(barberService.updateBaber(barber))
            return("OK");
        else
            return("FAIL");
    }

    @GetMapping("/getinfo")//个人信息
    Map getinfo(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map) throws IOException {
        logger.debug(map);
        Map	islogin= (Map) request.getSession().getAttribute("islogin");
        logger.debug(islogin);
        map.putAll(islogin);
        logger.debug(map);
        Map m=barberService.getinfo(map);
        logger.debug(m);
        if (m!=null) {
            return m;
        } else {
            return null;
        }
    }
}
